/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/**
 *
 * @author zimi
 */

public class Recipe {

    public Recipe() {
    }

    public Recipe(KXmlParser parser) throws IOException, XmlPullParserException {
        while (parser.nextTag() != XmlPullParser.END_TAG) {
            parser.require(XmlPullParser.START_TAG, null, null);
            String name = parser.getName();
            name = parser.getName();
            String text = null;
            if(!(name.equals("ingredients")||name.equals("activities")))
                text = parser.nextText();
            if (name.equals("name")) {
                this.name = text;
            } else if (name.equals("description")) {
                this.description =text;
            } else if (name.equals("picturePath")) {
                this.picturePath=text;
            } else if (name.equals("portions")) {
                this.setPortions(Integer.parseInt(text));
            } else if (name.equals("recently")) {
                if (text.equals("true")){
                    this.recently = true;
                } else {
                    this.recently = false;
                }
            }
            else if (name.equals("favorite")) {
                if (text.equals("true")){
                    this.favorite=true;
                } else {
                    this.favorite=false;
                }
            } else if (name.equals("category")) {
                this.category =  text;//new Category(parser);
            } else if (name.equals("ingredients")) {
                while (parser.nextTag() != XmlPullParser.END_TAG) {
                    parser.require(XmlPullParser.START_TAG, null, null);
                    this.addIngredient(new Ingredient(parser));
                    parser.require(XmlPullParser.END_TAG, null, null);
                }
            } else if(name.equals("activities")) {
                while (parser.nextTag() != XmlPullParser.END_TAG) {
                    parser.require(XmlPullParser.START_TAG, null, null);
                    this.addActivity(new Activity(parser));
                    parser.require(XmlPullParser.END_TAG, null, null);
                }
            }
            parser.require(XmlPullParser.END_TAG, null, null);
        }
    }

    private Vector/*<Ingredient>*/ ingredients = new Vector();

    private Vector/*<Activity>*/ activities = new Vector();

    private String description;

    private String picturePath;

    private String category;

    private String name;

    private boolean favorite;

    private int portions;

    // TODO wydzielenie do profilu użytkownika
    boolean recently;

    public double calculateCaloriesOnPortion() {
        throw new RuntimeException("todo");
    }

//    public Category getCategory() {
//        return category;
//    }
//
//    public void setCategory(Category category) {
//        this.category = category;
//    }
     public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isFavorite() {
        return favorite;
    }

    public void setFavorite(boolean favorite) {
        this.favorite = favorite;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPicturePath() {
        return picturePath;
    }

    public void setPicturePath(String picturePath) {
        this.picturePath = picturePath;
    }

    public int getPortions() {
        return portions;
    }

    public void setPortions(int portions) {
        this.portions = portions;
    }

    public boolean isRecently() {
        return recently;
    }

    public void setRecently(boolean recently) {
        this.recently = recently;
    }

    public boolean addIngredient(Ingredient ingredient) {
        ingredients.addElement(ingredient);
        return true;
    }

    public boolean removeIngredient(Ingredient ingredient) {
        return ingredients.removeElement(ingredient);
    }

    public Vector/*<Ingredient>*/ listIngredients() {
        return ingredients;
    }

    public Vector/*<Ingredient>*/ getIngredients() {
        return ingredients;
    }

    public boolean addActivity(Activity activity) {
        activities.addElement(activity);
        return true;
    }

    public boolean removeActivity(Activity activity) {
        return activities.removeElement(activity);
    }

    public Vector/*<Activity>*/ listActivities() {
        return activities;
    }

    public Vector/*<Activity>*/ getActivities() {
        return activities;
    }

    public String toString() {
        return getName();
    }

    public boolean containsProduct(Product searchedProduct) {
        for(int i = 0; i < ingredients.size(); i++) {
            Ingredient ingredient = (Ingredient)ingredients.elementAt(i);
            Product product = ingredient.getProduct();
            if(product.equals(searchedProduct)) {
                return true;
            }
        }
        return false;
    }

}
